import com.sun.deploy.panel.DeleteFilesDialog;
import com.sun.org.apache.xerces.internal.xs.StringList;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.im.spi.InputMethodDescriptor;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.PrivateKey;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: Nervaner
 * Date: 01.12.11
 * Time: 21:03
 * To change this template use File | Settings | File Templates.
 */
public class ImageFormer implements ActionListener {
    private int[] pixels;
    private int width;
    private int height;
    private BufferedImage image;
    private JFrame frame;

    public ImageFormer() {
        ImagePanel imagePanel = new ImagePanel();
        /*
        try {
            image = ImageIO.read(new File("picture.jpg"));
            width = image.getWidth();
            height = image.getHeight();
            imagePanel.setImage(image);
            pixels = new int[width * height];
            image.getRGB(0, 0, width, height, pixels, 0, width);
        } catch (IOException e) {
            e.printStackTrace();
        }         */
        //считывание изображения из файла формата pro..
        try {
            FileInputStream in = new FileInputStream("test.pro");
            in.skip(270);
            width = in.read()+ (in.read() << 8);
            height = in.read()+ (in.read() << 8);
            in.skip(238);
            pixels = new int[width * height];
            for (int i = 0; i < height; ++i)
                for (int j = 0; j < width; ++j) {
                    int c = in.read();
                    pixels[i * width + j] = c + (c << 8) + (c << 16);
                }
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            RefreshImage();
            imagePanel.setImage(image);
            imagePanel.setPreferredSize(new Dimension(width, height));
        } catch (IOException e) {
            e.printStackTrace();
        }
        frame = new JFrame("Some frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setPreferredSize(new Dimension(width, height));

        //кнопка применения фильтра
        JButton b = new JButton("Apply");
        b.setActionCommand("apply button");
        b.addActionListener(this);
        b.setAlignmentX(Component.CENTER_ALIGNMENT);
        b.setMaximumSize(new Dimension(80, 25));
        //b.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        //панель фильтров
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        //подсветка панели инструментов
        panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));


        //выпадающий список выбора фильтра
        JComboBox combo = new JComboBox();
        combo.setMaximumSize(new Dimension(300, 25));
        combo.addItem(new String("HighFrequency1"));

        panel.add(combo);
        panel.add(Box.createVerticalStrut(5));
        panel.add(b);
        frame.add(imagePanel, BorderLayout.WEST);
        frame.add(panel, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public void actionPerformed(ActionEvent e) {
        if (image != null) {
            if (frame.getContentPane().get )
            FilterHighFrequency1();
        }
        frame.repaint();
    }

    private void RefreshImage() {
        image.setRGB(0, 0, width, height, pixels, 0, width);
    }


    double ApertureMid(int x, int y, int n, int m) {
        double result = 0;
        for (int i = y - n / 2; i < y + n / 2 + 1; ++i)
            for (int j = x - m / 2; j < x + m / 2 + 1; ++j)
                result += pixels[i * width + j] >> 16;
        return result / (n * m);
    }

    private void FilterHighFrequency1() {
        //усиление высоких частот
        int sizeX = 3, sizeY = 3;   //размеры апертуры
        double k = 0.66;
        double mid;
        int res;
        int p[] = new int[width * height];
        for (int i = 0; i < height; ++i)
            for (int j = 0; j < width; ++j) {
                if (i > (sizeY / 2 - 1) && i < (height - sizeY / 2) && j > (sizeX / 2 - 1) && j < (width - sizeX / 2)) {
                    mid = ApertureMid(j, i, sizeY, sizeX);
                    mid = mid + k * ((pixels[i*width + j] >> 16) - mid);
                    res = (int)(mid);
                    if (res < 0)
                        res = 0;
                    if (res > 255)
                        res = 255;
                    res = res + (res << 8) + (res << 16);
                    p[i*width + j] = res;
                } else
                    p[i*width + j] = pixels[i*width + j];
            }
        pixels = p;
        RefreshImage();
    }


}
